// pages/pay/pay.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */

  data: {
    orderid:"",
    ordercode:"",
    OrderType:"",
    // Token_ID:"",
    allmsg:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000',
    })
    var orderid = options.orderid
    var ordercode = options.ordercode
    var OrderType = options.OrderType
    this.setData({
      orderid: orderid,
      ordercode: ordercode,
      OrderType: OrderType
    })

    this.getallmsg()
  },

  // gettokenid:function(){
  //   ajax('/api/HFBTokenID/Get?requestContent=' + JSON.stringify({
  //     token: wx.getStorageSync('Access_token'),
  //     OrderID: this.data.orderid,
  //     Vtype:0,
  //     OrderCode: this.data.ordercode

  //   }), {
  //       header: {
  //         'Accept': 'application/x-www-form-urlencoded', // 默认值
  //       },
  //       method: "get",
  //     }, res => {

  //         console.log(res)

  //         this.setData({
  //           Token_ID: res.Token_ID
  //         })
  //     })
  // },

  topaysuccess: function () {
    var orderid = this.data.orderid
    var allmoney = this.data.allmsg.Money
    wx.requestPayment({
      timeStamp: this.data.allmsg.timeStamp,
      nonceStr: this.data.allmsg.nonce_str,

      package: this.data.allmsg.prepay_id,

      signType: this.data.allmsg.sign_type,
      paySign: this.data.allmsg.sign,

      success(res) {
        console.log(res)
        wx.redirectTo({
          url: '/pages/paysuccess/paysuccess?orderid=' + orderid + '&allmoney=' + allmoney,
        })
      },
      fail(res) {
        console.log(res)
      }
    })
  },

  getallmsg:function(){
    // wx.navigateTo({
    //   url: '/pages/paysuccess/paysuccess',
    // })

    ajax('/api/Pay/Get?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      OrderID: this.data.orderid,
      OrderCode: this.data.ordercode,
      OrderType: this.data.OrderType,
      code: wx.getStorageSync('code')
    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "get",
      }, res => {

        if (res.Models[0]){
            console.log(res)
            this.setData({
              allmsg: res.Models[0]
            })
          }
      })
  },

  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})